﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using BUS;
using DTO;
namespace AirPlane
{
    public partial class DatVeB3 : System.Web.UI.Page
    {
        protected VeMayBayBUS bus = new VeMayBayBUS();
        protected DataTable dt = new DataTable();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["islogin"] == null || (bool)Session["islogin"] == false || Session["type"].ToString() != "1")
            {

                Response.Redirect("TrangChu.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    if (Request["chuyenbaydi"].ToString() == "" || Request["hangvedi"].ToString() == "" || Request["sum"].ToString() == "" || Request["doiTuong"].ToString() == "" || Session["khachHang"].ToString() == "")
                    {
                        Response.Redirect("DatVe.aspx");
                    }
                    else
                    {
                        string maChuyenBayDi = Request["chuyenbaydi"].ToString();
                        string maChuyenBayVe = Request["chuyenbayve"].ToString();
                        int hangVeDi = int.Parse(Request["hangvedi"].ToString());
                        int hangVeVe = int.Parse(Request["hangveve"].ToString());
                        float tong = 0;
                        tong = float.Parse(Request["sum"].ToString());
                        float thue = 0;
                        thue = tong * 10 / 100;
                        float tongTien = tong + thue;
                        lbGia.Text = tong.ToString();
                        lbThue.Text = thue.ToString();
                        lbTong.Text = tongTien.ToString();
                        loadData(maChuyenBayDi, maChuyenBayVe, hangVeDi, hangVeVe);
                    }
                   
                }
            }
        }
        public void loadData(string maChuyenBayDi, string maChuyenBayVe, int hangVeDi, int hangVeVe)
        {
            DataTable dt1 = new DataTable();
            DataTable dt2 = new DataTable();
           string s = "";
            dt1 = bus.layThongTinDatVeB3(maChuyenBayDi, hangVeDi);

            foreach (DataRow dr1 in dt1.Rows)
            {
                string thuTrongTuanKhoiHanh =  dr1["ngayTrongTuanKhoiHanh"].ToString();
                 string thuTrongTuanDen =  dr1["ngayTrongTuanDen"].ToString();
                string ngayKhoiHanh = dr1["ngayDi"].ToString();
                string ngayDen = dr1["ngayDen"].ToString();
                string gioDi =  dr1["gioDi"].ToString();
                string gioDen =  dr1["gioDen"].ToString();
                string thanhPhoDi = dr1["thanhPhoDi"].ToString();
                string quocGiaDi = dr1["quocGiaDi"].ToString();
                string ID_sanBayDi = dr1["ID_sanBayDi"].ToString();
                string thanhPhoDen = dr1["thanhPhoDen"].ToString();
                string quocGiaDen = dr1["quocGiaDen"].ToString();
                string ID_sanBayDen = dr1["ID_sanBayDen"].ToString();
                string loaiVeDi = dr1["loaiVe"].ToString();

                s += "<table align='center' class='style3'>";
                s += "<tr>";
                s +=  "<td class='style4'>";
                s += "<span style='color: #166292;font-weight:bold'>Đi</span></td>";
                s += "<td class='style5'>";
                s += thuTrongTuanKhoiHanh + "," + ngayKhoiHanh + "</td>";
                s += "<td style='text-align:right'>";
                s += "<span style='color: #166292;font-weight:bold'>Singapore Airline</span></td>";
                s += "</tr>";
                s += "<tr>";
                s += "<td class='style4'>";
                s +=  gioDi + "</td>";
                s +=  "<td class='style5'>";
                s +=  thanhPhoDi + ", " + quocGiaDi + " ("+ ID_sanBayDi + ")" +"</td>";
                s +=  "<td style='text-align:right'>";
                s += "Loại Vé: " + loaiVeDi + "</td>";
                s += "</tr>";
                s += "<tr>";
                s += "<td class='style'>";
                s += "<span style='color: #166292;font-weight:bold'> Đến</span></td>";
                s += "<td class='style5'>";
                s += thuTrongTuanDen + ", " + ngayDen + "</td>";
                s +="<td style='text-align:right'>";
                s+=  dr1["soHieuMayBay"].ToString() + "</td>";
                s += "</tr>";
                s += "<tr>";
                s += "<td class='style4'>";
                s +=  gioDen  +"</td>";
                s += "<td class='style5'>";
                s +=  thanhPhoDen + ", " + quocGiaDen + " ("+ ID_sanBayDen + ")" +"</td>";
               s += "<td style='text-align:right'>";
               s +="Bay Thẳng</td>";
               s += "</tr>";
               s += "</table>";
               s += "<div id='booking-insert-step3'></div>";
            }
            if (maChuyenBayVe != "")
            {
                dt2 = bus.layThongTinDatVeB3(maChuyenBayVe, hangVeVe);
                foreach (DataRow dr2 in dt2.Rows)
                {
                    string thuTrongTuanKhoiHanh = dr2["ngayTrongTuanKhoiHanh"].ToString();
                    string thuTrongTuanDen = dr2["ngayTrongTuanDen"].ToString();
                    string ngayKhoiHanh = dr2["ngayDi"].ToString();
                    string ngayDen = dr2["ngayDen"].ToString();
                    string gioDi = dr2["gioDi"].ToString();
                    string gioDen = dr2["gioDen"].ToString();
                    string thanhPhoDi = dr2["thanhPhoDi"].ToString();
                    string quocGiaDi = dr2["quocGiaDi"].ToString();
                    string ID_sanBayDi = dr2["ID_sanBayDi"].ToString();
                    string thanhPhoDen = dr2["thanhPhoDen"].ToString();
                    string quocGiaDen = dr2["quocGiaDen"].ToString();
                    string ID_sanBayDen = dr2["ID_sanBayDen"].ToString();
                    string loaiVeDi = dr2["loaiVe"].ToString();

                    s += "<table align='center' class='style3'>";
                    s += "<tr>";
                    s += "<td class='style4'>";
                    s += "<span style='color: #166292;font-weight:bold'>Đi</span></td>";
                    s += "<td class='style5'>";
                    s += thuTrongTuanKhoiHanh + "," + ngayKhoiHanh + "</td>";
                    s += "<td style='text-align:right'>";
                    s += "<span style='color: #166292;font-weight:bold'>Singapore Airline</span></td>";
                    s += "</tr>";
                    s += "<tr>";
                    s += "<td class='style4'>";
                    s += gioDi + "</td>";
                    s += "<td class='style5'>";
                    s += thanhPhoDi + ", " + quocGiaDi + " (" + ID_sanBayDi + ")" + "</td>";
                    s += "<td style='text-align:right'>";
                    s += "Loại Vé: " + loaiVeDi + "</td>";
                    s += "</tr>";
                    s += "<tr>";
                    s += "<td class='style'>";
                    s += "<span style='color: #166292;font-weight:bold'> Đến</span></td>";
                    s += "<td class='style5'>";
                    s += thuTrongTuanDen + ", " + ngayDen + "</td>";
                    s += "<td style='text-align:right'>";
                    s += dr2["soHieuMayBay"].ToString() + "</td>";
                    s += "</tr>";
                    s += "<tr>";
                    s += "<td class='style4'>";
                    s += gioDen + "</td>";
                    s += "<td class='style5'>";
                    s += thanhPhoDen + ", " + quocGiaDen + " (" + ID_sanBayDen + ")" + "</td>";
                    s += "<td style='text-align:right'>";
                    s += "Bay Thẳng</td>";
                    s += "</tr>";
                    s += "</table>";
                }
            }
            lbChuyenBay.Text = s;
            
        }
        protected void btnHuyChuyenBay_Click(object sender, EventArgs e)
        {
            Response.Redirect("DatVe.aspx");
        }

        protected void btnDatVeXacNhan_Click(object sender, EventArgs e)
        {
            string maVeDi = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Millisecond.ToString();
            string maVeVe = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + (DateTime.Now.Millisecond + 10).ToString();
            string maChuyenBayDi = Request["chuyenbaydi"].ToString();
            string maChuyenBayVe = Request["chuyenbayve"].ToString();
            int hangVeDi = int.Parse(Request["hangvedi"].ToString());
            int hangVeVe = int.Parse(Request["hangveve"].ToString());
            int doiTuong = int.Parse(Request["doiTuong"].ToString());
            if (Session["khachHang"].ToString() == "")
            {
                result_anounment.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Vé này đã thanh toán rồi!</span>";
            }
            else
            {
                KhachHangDTO kh = new KhachHangDTO();
                kh = (KhachHangDTO)Session["khachHang"];
                bus.datVe(kh, maVeDi, maChuyenBayDi, hangVeDi, maVeVe, maChuyenBayVe, hangVeVe, doiTuong);
                Session["khachhang"] = "";
                result_anounment.Text = "<span id = 'thong-bao-ket-Qua-Tim-Kiem'>Đã Đặt Vé Thành Công!</span>";
            }
        }
    }
}